﻿using SharPy.GameClasses;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

namespace SharPy.Interface
{
    class clGameInterfaceHelper
    {
        GamePage page;

        Grid gridMain;
        Grid gridText;
        TextBlock sMainText;
        TextBlock sMainTextWI;
        Image imBackground;
        Image imCharacter;
        TextBlock sBackground;
        Frame frameMain;
        TextBlock sHeader;
        TextBlock sHeaderTextWI;

        public clGameInterfaceHelper(GamePage CurrentPage)
        {
            this.page = CurrentPage;
            this.gridMain = (Grid)CurrentPage.FindName("gridMain");
            this.gridText = (Grid)CurrentPage.FindName("gridText");
            this.sMainText = (TextBlock)CurrentPage.FindName("sMainText");
            this.sMainTextWI = (TextBlock)CurrentPage.FindName("sMainTextWithImage");
            this.imBackground = (Image)CurrentPage.FindName("imBackground");
            this.imCharacter = (Image)CurrentPage.FindName("imCharacter");
            this.sBackground = (TextBlock)CurrentPage.FindName("sBackgroundText");
            this.frameMain = (Frame)CurrentPage.FindName("frameMain");
            this.sHeader = (TextBlock)CurrentPage.FindName("sHeaderText");
            this.sHeaderTextWI = (TextBlock)CurrentPage.FindName("sHeaderTextWithImage"); 
        }

        public int blockInterface(bool pBlock)
        {
            if (pBlock == true)
            {
                foreach (var element in gridText.Children)
                    element.PointerPressed -= page.sMainText_PointerPressed;
                return 1;
            }
            else
            {
                foreach (var element in gridText.Children)
                {
                    if (element.GetType() == typeof(TextBlock))
                        element.PointerPressed += page.sMainText_PointerPressed;
                }
                return 0;
            }
        }

        public async Task<int> ClearScreen(GamePage page)
        {
            await clImages.DeleteOldImages(page);
            
            
            ListBox menu = (ListBox)page.FindName("ActiveMenu");
            if (menu != null)
                gridMain.Children.Remove(menu);

            imBackground.Source = null;

            foreach (var element in gridText.Children)
            {
                if (element.GetType() == typeof(TextBlock))
                    (element as TextBlock).Text = "";
            }
            return 1;
        }

        internal async Task<int> LoadGame(GamePage page, GameObjects.clGame CurrentGame)
        {
            await ClearScreen(page);

            int i = 0;
            string s;
            SharPy.Parser.clCommand objCom = new SharPy.Parser.clCommand();

            while (i <= CurrentGame.LineNoHolder)
            {
                if ((s = SharPy.Parser.clText.getLine(i)) != null)
                    objCom = SharPy.Parser.clParser.lineMatch(s);
                else
                {
                    objCom.sName = SharPy.Parser.clCommand.Name.EndGame;
                }
                if (objCom.sName != Parser.clCommand.Name.ShowMenu)
                    if(objCom.sName != Parser.clCommand.Name.JumpToLine)
                        if(objCom.sName!=Parser.clCommand.Name.JumpToLabel)
                        await page.CommandHandle(objCom);
                i++;
            }

            return 1;
        }
    }
}
